package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.JiaoFuJiHuaDao;
import cn.gson.boot.model.dao.JiaoFuJiHuaXiangQinDao;
import cn.gson.boot.model.dao.ShenHeDao;
import cn.gson.boot.model.mapper.JiaoFuJiHuaMapper;
import cn.gson.boot.model.mapper.JiaoFuJiHuaXiangQingMapper;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class JiaoFuJiHuaService {
    @Autowired
    JiaoFuJiHuaDao jiaoFuJiHuaDao;//交付计划Dao
    @Autowired
    JiaoFuJiHuaMapper jiaoFuJiHuaMapper;
    @Autowired
    JiaoFuJiHuaXiangQinDao jiaoFuJiHuaXiangQinDao;//交付计划详情Dao
    @Autowired
    JiaoFuJiHuaXiangQingMapper jiaoFuJiHuaXiangQingMapper;

    @Autowired
    ShenHeDao shenHeDao;

    //新增交付计划
    public void addJiaoFuJiHua(Jiaofujihua jiaofujihua){
        jiaofujihua.setJjYs(1);
        Jiaofujihuaxiangqing dx = null;
        if(jiaofujihua.getJjId()!=0){
            jiaoFuJiHuaXiangQinDao.deleteJiaoFuJiHuaXiangQing(jiaofujihua.getJjId());
            jiaoFuJiHuaDao.save(jiaofujihua);
        }else if(jiaofujihua.getJjId() == 0){
            Jiaofujihua savev =  jiaoFuJiHuaDao.save(jiaofujihua);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(savev.getJjId());
            shenhe.setShShouming("交付计划" );
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setShenqingPeople(savev.getJjSye());
            System.err.println("有包：" + shenhe.getShZhuangtai());
            shenHeDao.save(shenhe);
        }
        for (int i=0;i<jiaofujihua.jjDingDanxqList.size();i++) {
          //  System.out.println("产品名称id：" + jiaofujihua.jjDingDanxqList.get(i).getProduct().getProductName());
            dx = new Jiaofujihuaxiangqing();

            dx.setProductt(jiaofujihua.jjDingDanxqList.get(i).getProductt());
            dx.setJfjhxqNum(jiaofujihua.jjDingDanxqList.get(i).getDdcpNum());
            dx.setJfjhxqPrice(jiaofujihua.jjDingDanxqList.get(i).getDdcpPrice());
            dx.setJiaofujihuaByJjId(jiaofujihua);
            dx.setJfjhXiaoJi(jiaofujihua.jjDingDanxqList.get(i).getDdxqXiaoji());

            jiaoFuJiHuaXiangQinDao.save(dx);
        }
    }

    //查看所有交付计划
    public List<Jiaofujihua> selectAllJiaoFuJiHua(String ddHao,String khName,String time,String empName) {
        return jiaoFuJiHuaMapper.selectAllJiaoFuJiHua(ddHao,khName,time,empName);
    }

    //移除交付计划
    public String yichuJiaoFuJiHua(Integer jjId) {
        String fh = null;
        Jiaofujihua jiaofujihua = jiaoFuJiHuaMapper.selectJiaoFuJiHuaByjjId(jjId);
        //如果订单的审核状态为待审核，就可以进行物理删除
        //并且在审核表里面也要删除这条交付计划的审核
        if(jiaofujihua.getJjTixing().equals("待审核")){
            shenHeDao.delectShenHe(jjId,"交付计划");
            jiaoFuJiHuaXiangQinDao.deleteJiaoFuJiHuaXiangQing(jjId);
            jiaoFuJiHuaDao.deleteJiaoFuJiHua(jjId);
            fh = "删除成功";
        }else if(jiaofujihua.getJjTixing().equals("否决")){
            //审核状态为否决就可以移除
            jiaoFuJiHuaDao.yichuJiaoFuJiHua(jjId);
            fh = "移除成功";
        }else {
            fh = "移除失败，此交付计划已被执行";
        }

        return fh;
    }

    //根据订单id查询交付计划详情
    public List<Jiaofujihuaxiangqing> selectJJXQbyDdId(Integer ddId){
        return jiaoFuJiHuaXiangQingMapper.selectJJXQbyDdId(ddId);
    }

    //根据交计id找到交计详情
    public List<Jiaofujihuaxiangqing> ckJJXQbyjjId(Integer jjId) {
        return jiaoFuJiHuaXiangQingMapper.ckJJXQbyjjId(jjId);
    }

    //根据交付计划id查看交付计划
    public Jiaofujihua selectJiaoFuJiHuaByjjId(Integer jjId){
        return jiaoFuJiHuaMapper.selectJiaoFuJiHuaByjjId(jjId);
    }

    //根据选择的订单查看此订单交付计划状态为同意的交付计划
    public List<Jiaofujihua> fhckddJiaoJi(Integer ddId) {
        return jiaoFuJiHuaMapper.fhckddJiaoJi(ddId);
    }
}